#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class TeachMissionProvider : CY.GFive.Core.Providers.DALProvider.ITeachMissionProvider
	{
		#region Sql
		private static readonly string SqlSelectTeachMission = "SELECT [Id]  FROM [TeachMission] WHERE  [Id]=@Id";

		private static readonly string SqlInsertTeachMission = "INSERT INTO [TeachMission]() values();SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateTeachMission = "UPDATE [TeachMission] SET  WHERE  [Id]=@Id";

		private static readonly string SqlDeleteTeachMission = "DELETE [TeachMission] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllTeachMission = "SELECT [Id]  FROM [TeachMission]";
		#endregion

		#region ITeachMissionProvider Members
		public Core.Business.TeachMission Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectTeachMission);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.TeachMission teachMission = new Core.Business.TeachMission();

				if (!reader.IsDBNull(0)) teachMission.Id = reader.GetInt32(0);

				return teachMission;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.TeachMission teachMission)
		{
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertTeachMission);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) teachMission.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.TeachMission teachMission)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, teachMission.Id);
			sql.ExecuteSql(SqlUpdateTeachMission);
		}

		public void Delete(Core.Business.TeachMission teachMission)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, teachMission.Id);
			sql.ExecuteSql(SqlDeleteTeachMission);
		}

		public IList<Core.Business.TeachMission> GetAllTeachMission()
		{
			IList<Core.Business.TeachMission> teachMissionlist = new List<Core.Business.TeachMission>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllTeachMission);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.TeachMission teachMission = new Core.Business.TeachMission();

					if (!reader.IsDBNull(0)) teachMission.Id = reader.GetInt32(0);

					teachMission.MarkOld();
					teachMissionlist.Add(teachMission);
				}
				reader.Close();
			}
			return teachMissionlist;
		}
		#endregion
	}
}
